<template>
  <div class='courseList'>
    <el-card class="box-card" style="margin-bottom:20px">
      <div slot="header" class="clearfix">
        数据筛选
      </div>
      <el-form :inline="true" :model="filterParams" ref="form" class="demo-form-inline">
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="filterParams.courseName"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="filterParams.status">
            <el-option label="全部" value=""></el-option>
            <el-option label="上架" value="1"></el-option>
            <el-option label="下架" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            :disabled="loading"
            @click="handleReset"
          >重置</el-button>
          <el-button
            type="primary"
            :disabled="loading"
            @click="handleFilter"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="headerfix">
        <div>查询结果</div>
        <el-button
          type="primary"
          @click="$router.push({
            name: 'courseCreate'
          })"
        >添加课程</el-button>
      </div>
      <el-table
        :data="courses"
        v-loading="loading"
        style="width: 100%; margin-bottom: 20px"
      >
        <el-table-column
          prop="id"
          label="ID"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="courseName"
          label="课程名称"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="price"
          label="价格"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="sortNum"
          label="排序"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="status"
          label="上架状态"
          align="center"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              :disabled="scope.row.isStatusLoading"
              @change="onStateChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="price"
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              @click="$router.push({
                name: 'courseEditor',
                params: {
                  courseId: scope.row.id
                }
              })"
            >编辑</el-button>
            <el-button
              @click="$router.push({
                name: 'courseSection',
                params: {
                  courseId: scope.row.id
                }
              })"
            >内容管理</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      background
      class="pagination"
      layout="prev, pager, next"
      :total="totalCount"
      :disabled="loading"
      @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script lang='ts'>
import { getQueryCourses } from '@/services/course'
import { Form } from 'element-ui'
import Vue from 'vue'
export default Vue.extend({
  name: 'CourseList',
  data () {
    return {
      filterParams: {
        currentPage: 1,
        pageSize: 10,
        courseName: '',
        status: ''
      },
      courses: [],
      totalCount: 0,
      loading: true
    }
  },
  mounted () {
    this.initCourses()
  },
  methods: {
    async initCourses () {
      this.loading = true
      const { data } = await getQueryCourses(this.filterParams)
      data.data.records.forEach((item: any) => {
        item.isStatusLoading = false
      })
      this.courses = data.data.records
      this.totalCount = data.data.total
      this.loading = false
    },
    handleReset () {
      (this.$refs.form as Form).resetFields()
      this.filterParams.currentPage = 1
      this.initCourses()
    },
    handleFilter () {
      this.filterParams.currentPage = 1
      this.initCourses()
    },
    handleCurrentChange (page: number) {
      this.filterParams.currentPage = page
      this.initCourses()
    }
  }
})
</script>

<style lang='scss' scoped>
.headerfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.pagination {
  display: flex;
  justify-content: flex-end;
}
</style>
